'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:ManageChooseinfoCtrl
 * @description
 * # ManageChooseinfoCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('ManageChooseinfoCtrl', function ($scope, $rootScope, $window, $routeParams, Util, openApi, localStorageService, PagingService) {
        var relModuleObj;
        // 获取搜索所需参数以及标题信息
        relModuleObj = Util.getDataforsearch();
        $scope.picker = angular.extend($scope.picker||{}, relModuleObj.param || {});
        $scope.selectLists = []; //已经选择了的数组，用来回传
        // -----------------底部款项类型选框添加end-----------------------
        $scope.choosePeople = function (index) {
            var curItemIndex;
            var curItem;
            curItem = $scope.taskList.items[index]
            curItemIndex = $scope.checkIfExsit(index);
            if (curItemIndex == -1) {
                $scope.selectLists.push(curItem)
            } else {
                $scope.selectLists.splice(curItemIndex, 1)
            }
            return $scope.selectLists;
        };
        $scope.checkIfExsit = function (index) {
            var selecrIndex = -1;
            var cerId;
            if (typeof index == "undefined") {
                return selecrIndex
            }
            cerId = $scope.taskList.items[index].cerId;
            $scope.selectLists.forEach(function (item, index) {
                if (item.cerId == cerId) {
                    selecrIndex = index;
                    return selecrIndex;
                }
            });
            return selecrIndex;
        };
        // 搜索
        $scope.search = function () {
            var url;
            url = getParamUrl();
            // param.certificateType = $scope.certificateType || "";
            $scope.taskList = new PagingService(url + "?canAppointment=1", $scope.picker);
        };
        // 根据不同的id请求的列表地址会不同
        var getParamUrl = function () {
            var url, form_id;
            form_id = $routeParams.id;
            if (form_id == "hzj") {
                url = openApi.getAllCertificateA;
            } else if (form_id == "jzj") {
                url = openApi.getAllCertificate;
            } else if (form_id == "zjdp") {
                url = openApi.certificateShift;
            }
            return url;
        };
        // 获取数据返回
        $scope.submit = function () {
            // 缓存中的数据
            var localSelectLists;
            if ($scope.selectLists.length < 1) {
                Util.Toast("请选择");
                return;
            }
            localSelectLists = Util.getSelectLists();
            $scope.selectLists = Util.mergeArr($scope.selectLists, localSelectLists, "cerId")
            localStorageService.set("selectLists", $scope.selectLists);
            $window.history.back();
        };
        // 过滤逻辑
        var hideDialog = function () {
            $scope.picker = angular.extend({}, relModuleObj.param || {});
            $rootScope.showMasker = false;
            $scope.showCerPicker = false;
        };
        $scope.cacelDialog = function () {
            hideDialog();
        };
        // 验证是否输入完整
        $scope.confirmDialog = function () {
            $scope.search()
            hideDialog();
        };
        var showDialog = function () {
            var param = {};
            param.type = "证件类别";
            Util.getcerTypeList(param, function (data) {
                $scope.resiTypeList = data.data
                $rootScope.showMasker = true;
                $scope.showCerPicker = true;

            });

        };

        $scope.searchID = function () {
            showDialog()
        }

        // 页面初始化请求数据
        var init = function () {
            $scope.search();
        };
        init();
    });
